package com.ruoyi.project.good.controller;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.good.domain.GoodSpec;
import com.ruoyi.project.good.domain.Goods;
import com.ruoyi.project.good.domain.GoodsPhoto;
import com.ruoyi.project.good.service.IGoodHisService;
import com.ruoyi.project.good.service.IGoodSpecService;
import com.ruoyi.project.good.service.IGoodsPhotoService;
import com.ruoyi.project.good.service.IGoodsService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品Controller
 *
 * @author ruoyi
 * @date 2020-07-01
 */
@RestController
@RequestMapping("/good/goods")
public class GoodsController extends BaseController {
    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IGoodsPhotoService goodsPhotoService;

    @Autowired
    private IGoodSpecService specService;

    @Autowired
    private IGoodHisService goodHisService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('good:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods) {
        startPage();

        List<Goods> list = goodsService.selectGoodsList(goods);
        for (Goods g : list) {
            List<GoodsPhoto> photos = goodsPhotoService.selectGoodsPhotosByGoodId(g.getGoodsId());
            g.setPhotoList(photos);

            List<GoodSpec> specs = specService.selectGoodSpecListByGoodId(g.getGoodsId());
            g.setSpecList(specs);
        }
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('good:goods:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Goods goods) {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('good:goods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId) {
        Goods g = goodsService.selectGoodsById(goodsId);
        List<GoodsPhoto> photos = goodsPhotoService.selectGoodsPhotosByGoodId(g.getGoodsId());
        g.setPhotoList(photos);

        List<GoodSpec> specs = specService.selectGoodSpecListByGoodId(g.getGoodsId());
        g.setSpecList(specs);
        return AjaxResult.success(g);
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('good:goods:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        goods.setUserId(user.getUserId());
        goods.setCompanyId(user.getCompanyId());
        goodHisService.addHis(user, "新增商品：" + goods.getGoodsName(), "");
        return toAjax(goodsService.insertGoods(goods));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('good:goods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();

        goodHisService.addHis(user, "修改商品：" + goods.getGoodsName(), "商品id：" + goods.getGoodsId());
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('good:goods:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsId}")
    public AjaxResult remove(@PathVariable Long goodsId) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();

        goodHisService.addHis(user, "删除商品", "商品id：" + goodsId);
        return toAjax(goodsService.deleteGoodsById(goodsId));
    }
}
